package com.canal.starter.config;

import com.canal.starter.transfer.MessageTransponders;
import com.canal.starter.client.CanalClient;
import com.canal.starter.client.SimpleCanalClient;
import com.canal.starter.util.ApplicationContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
 * @author chen.qian
 * @date 2018/3/19
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(CanalProperties.class)
public class CanalClientConfiguration {
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public ApplicationContextUtils applicationContextUtils() {
        return new ApplicationContextUtils();
    }

    @Bean
    public CanalClient canalClient(CanalProperties canalProperties) {
        log.info("初始化 Canal 客户端...");
        CanalClient canalClient = new SimpleCanalClient(canalProperties, MessageTransponders.defaultMessageTransponder());
        canalClient.start();
        return canalClient;
    }
}
